package aad.storyboard.controller;

import java.util.ArrayList;

import aad.storyboard.model.Animation;
import aad.storyboard.model.AnimationProvider;
import aad.storyboard.model.DragDropAnimation;
import aad.storyboard.model.Image;
import aad.storyboard.model.InfoAnimation;
import aad.storyboard.model.Story;
import aad.storyboard.view.GraphicView;
import android.graphics.Color;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class ViewTouchListener implements OnTouchListener{
	
	private Story story;	
	

	public ViewTouchListener(Story s){
		story = s;
	}
	
/**
 * 	triggered on touch	
 */
	public boolean onTouch(View v, MotionEvent event) {		
		NavigationController.getInstance().handleOnTap(event);
		checkTouchAnimations(event);
		checkDragAnimations(event);		
		
		return false;
	}
	/**
	 * Checks Drag and Drop Animations to be performed at touch, in case a touch event is fired. The Animation is triggered only when the touched point is a nontransparent pixel of an animatable image.
	 */
	private void checkDragAnimations(MotionEvent event) {
		//Drag & Drop
		float evX = event.getX();
		float evY = evX;
		Integer key1 = new Integer(AnimationProvider.EVENT_DRAG);
		ArrayList<Animation> dragAnimations = AnimationProvider.getInstance().getOnEventAnimations().get(key1);
		if(dragAnimations != null){
			for(Animation a : dragAnimations){
				for(Image img : a.getImages()){
					if(story.getCurrentChapter().getCurrentScene().containsImage(img.getName())){
						if(evX>= img.getPosX() && evX <= img.getPosX()+img.getWidth()){
							if(evY>= img.getPosY() && evY <= img.getPosY()+img.getHeight()){
								//check
								int color = img.getBitmap().getPixel(Math.round(evX), Math.round(evY));
								if(Color.alpha(color) < 20){
									int executionTime = StoryTimer.getInstance().getCurrentTime();
									Log.e("TAG", "FOUND ANIMATION TO STRT"+a);
									int eventaction = event.getAction(); 
							        int X = (int)event.getX(); 
							        int Y = (int)event.getY();
							        ((DragDropAnimation)a).setCoordinates(X,Y);
							        ((DragDropAnimation)a).setEventaction(eventaction);
							        ((DragDropAnimation)a).setNewTouch(true);								
									((DragDropAnimation)a).start();
									ControllerManager.getInstance().getFRC().addObserver(a);
									AnimationProvider.getInstance().addAnimationToStack(a);
									break;
								}
							}
						}	
					}
				}
			}
		}
		
	}

	/**
	 * Checks Animations to be performed at touch, in case a touch event is fired. The Animation is triggered only when the touched point is a nontransparent pixel of an animatable image.
	 */
	private void checkTouchAnimations(MotionEvent event) {
		float evX = event.getX();
		float evY = evX;
		Integer key = new Integer(AnimationProvider.EVENT_TOUCH);
		ArrayList<Animation> touchAnimations = AnimationProvider.getInstance().getOnEventAnimations().get(key);
		if(touchAnimations != null){
			for(Animation a : touchAnimations){
				for(Image img : a.getImages()){
					
					if(story.getCurrentChapter().getCurrentScene().containsImage(img.getName())){						
						if(evX>= img.getPosX() && evX <= img.getPosX()+img.getWidth()){
							if(evY>= img.getPosY() && evY <= img.getPosY()+img.getHeight()){
								//check
								int color = img.getBitmap().getPixel(Math.round(evX), Math.round(evY));
								if(Color.alpha(color) < 95){
									int executionTime = StoryTimer.getInstance().getCurrentTime();
									Log.e("TAG", "FOUND ANIMATION TO STRT"+a);
									AudioPlayer.getInstance().pauseAudio();
							    	//AnimationProvider.getInstance().pauseAllActiveAnimations();		
									if (a instanceof InfoAnimation)
										{																
										((InfoAnimation)a).setCurrentImage(img);
										((InfoAnimation)a).setTouched(true);
										}
									a.start();
									ControllerManager.getInstance().getFRC().addObserver(a);
									AnimationProvider.getInstance().addAnimationToStack(a);
									break;
								}
							}
						}	
					}
				}
			}
		}
		
		
	}

	


}
